2 Problem: 314 - Robot (UVa)
3 Author: Andrés Mejía-Posada
4 (http://blogaritmo.factorcomun.org)
28 #define D(x) cout << #x " is " << x << endl
33 state(){} state(short i
, short j
, short d
, short w
) : i(i
), j(j
), d(d
), w(w
) {}
36 int di
[] = {-1, +0, +1, +0}, dj
[] = {+0, +1, +0, -1};
40 int g
[N
+1][N
+1], t
[N
+2][N
+2], n
, m
;
44 short si
, sj
, fi
, fj
; string dir
;
45 cin
>> si
>> sj
>> fi
>> fj
>> dir
;
48 case 'n': sd
= 0; break;
49 case 'e': sd
= 1; break;
50 case 's': sd
= 2; break;
51 case 'w': sd
= 3; break;
57 memset(v
, 0, sizeof v
);
58 q
.push(state(si
, sj
, sd
, 0));
62 short i
= f
.i
, j
= f
.j
, d
= f
.d
, w
= f
.w
;
67 if (i
< 0 || i
> n
|| j
< 0 || j
> m
|| !t
[i
][j
] || v
[i
][j
][d
]) continue;
69 if (i
== fi
&& j
== fj
){
70 //printf("popped %d %d %d %d\n",i,j,d,w);
75 //printf("popped %d %d %d %d\n",i,j,d,w);
78 q
.push(state(i
, j
, (d
+1)%4, w
+1));
79 q
.push(state(i
, j
, (d
+3)%4, w
+1));
80 for (int k
=1; k
<=3; ++k
){
81 int ni
= i
+k
*di
[d
], nj
= j
+k
*dj
[d
];
82 if (ni
< 0 || ni
> n
|| nj
< 0 || nj
> m
|| !t
[ni
][nj
]) break;
83 q
.push(state(ni
, nj
, d
, w
+1));
92 while (cin
>> n
>> m
&& (n
+m
)){
93 for (int i
=0; i
<n
; ++i
){
94 for (int j
=0; j
<m
; ++j
){
99 //Crear matriz t(ransformada)
100 for (int i
=0; i
<=n
; ++i
){
101 for (int j
=0; j
<=m
; ++j
){
102 if (i
== 0 || j
== 0 || i
== n
|| j
== m
) t
[i
][j
] = 0;
103 else t
[i
][j
] = !g
[i
-1][j
-1] && !g
[i
-1][j
] && !g
[i
][j
-1] && !g
[i
][j
];
104 //cout << t[i][j] << " ";